package com.ruoyi.framework.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.baomidou.mybatisplus.core.metadata.TableFieldInfo;
import com.ruoyi.common.core.domain.BaseEntity;
import com.ruoyi.framework.utils.LoginUserUtils;
import org.apache.ibatis.reflection.MetaObject;

import java.lang.reflect.Field;
import java.util.Date;
import java.util.Objects;

/**
 * 通用参数填充实现类
 *
 * 如果没有显式的对通用参数进行赋值，这里会对通用参数进行填充、赋值
 *
 * @author hexiaowu
 */
public class DefaultDBFieldHandler implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        Object originalObject = metaObject.getOriginalObject();
        if (originalObject instanceof BaseEntity) {
            BaseEntity baseEntity = (BaseEntity) originalObject;
            if (Objects.isNull(baseEntity.getCreateTime())) {
                baseEntity.setCreateTime(new Date());
            }
            if (Objects.isNull(baseEntity.getUpdateTime())) {
                baseEntity.setUpdateTime(baseEntity.getCreateTime());
            }
            if (Objects.isNull(baseEntity.getCreateBy())) {
                baseEntity.setCreateBy(LoginUserUtils.getLoginUserName());
            }
            if (Objects.isNull(baseEntity.getUpdateBy())) {
                baseEntity.setUpdateBy(baseEntity.getCreateBy());
            }
        } else {
            for (TableFieldInfo tableFieldInfo : findTableInfo(metaObject).getFieldList()) {
                if (tableFieldInfo.isWithInsertFill()) {
                    ifNullSetFieldValue(tableFieldInfo, metaObject);
                }
            }

        }
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        Object originalObject = metaObject.getOriginalObject();
        if (originalObject instanceof BaseEntity) {
            BaseEntity baseEntity = (BaseEntity) originalObject;
            if (Objects.isNull(baseEntity.getUpdateTime())) {
                baseEntity.setUpdateTime(new Date());
            }
            if (Objects.isNull(baseEntity.getUpdateBy())) {
                baseEntity.setUpdateBy(LoginUserUtils.getLoginUserName());
            }
        } else {
            for (TableFieldInfo tableFieldInfo : findTableInfo(metaObject).getFieldList()) {
                if (tableFieldInfo.isWithUpdateFill()) {
                    ifNullSetFieldValue(tableFieldInfo, metaObject);
                }
            }

        }
    }

    /**
     * 是空的就填充
     * @param tableFieldInfo
     */
    public void  ifNullSetFieldValue(TableFieldInfo tableFieldInfo,MetaObject metaObject ){
        Field field = tableFieldInfo.getField();
        Object fieldVal = getFieldValByName(field.getName(), metaObject);
        if (Objects.nonNull(fieldVal)){
            if (Date.class.isAssignableFrom(tableFieldInfo.getPropertyType())) {
                //是时间
                this.strictInsertFill(metaObject, field.getName(), Date.class, new Date());
            } else {
                //是用户了
                this.strictInsertFill(metaObject, field.getName(), String.class, LoginUserUtils.getLoginUserName());
            }
        }
    }

}
